package cn.factorybox;

import cn.factorybox.common.utils.DateFormatUtil;
import cn.factorybox.common.utils.IpUtils;
import cn.factorybox.common.utils.resultUtils.MatrixCalculate;
import cn.factorybox.item.service.DeviceDataAppService;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-03-15 16:34
 */
//@RunWith (SpringRunner.class)
@SpringBootTest
public class test {

    private static final Logger logger = LoggerFactory.getLogger(test.class);

    @Autowired
    private  DeviceDataAppService deviceDataAppService;

    @Test
    public void testCalculateTarget(){
        GregorianCalendar calendar = new GregorianCalendar();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int target = hour * 4 + minute/15;
        System.out.println(target);
    }

    @Test
    public void TestMatrix() {
        double[][] R = new double[5][4];
        double[][] weightValue = new double[1][5];
        double[][] result = new double[1][4];

        // 室内的权重
        weightValue[0][0] = 0.3928;
        weightValue[0][1] = 0.1229;
        weightValue[0][2] = 0.0508;
        weightValue[0][3] = 0.2632;
        weightValue[0][4] = 0.1704;

        double[] arr = {1,0,0,0};
        double[] arr1 = {0,1,0,0};
        double[] arr2 = {0,0,1,0};
        double[] arr3 = {0,0,0,1};
        double[] arr4 = {0,0,0,1};
        R[0] = arr;
        R[1] = arr1;
        R[2] = arr2;
        R[3] = arr3;
        R[4] = arr4;

        result = MatrixCalculate.matrixMultiply(weightValue,R);
        for (int i = 0; i < result[0].length; i++) {
            System.out.println(i + ":" + result[0][i]);
        }
    }

    @Test
    public void test2() {
        String[] fruits = {"apple", "banana", "xxx"};
        String iLikeEat = "apples";
        boolean isContains = Arrays.asList(fruits).contains(iLikeEat);
        System.out.println(isContains);
    }

    /**
     * 时间计算
     * @throws ParseException
     */
    @Test
    public void testTime() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Date time = sdf.parse("08:00");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.HOUR,1);
        System.out.println(DateFormatUtil.formatDeviceTime(calendar.getTime()));
    }

    @Test
    public  void test3() {
        Date dNow = new Date();
        Date before = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dNow);
        calendar.add(Calendar.DAY_OF_MONTH,-1);
        calendar.add(Calendar.DAY_OF_MONTH,-3);
        before = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowTime = sdf.format(dNow);
        String beforeTime = sdf.format(before);
        System.out.println("nowTime:" + nowTime);
        System.out.println("beforeTime:" + beforeTime);
    }

    /**
     * 测试线程
     */
    @Test
    public void threadTest() throws InterruptedException {
//        long startTime=System.currentTimeMillis();   //获取开始时间
//        for (int i = 0; i < 20; i++) {
//            deviceDataAppService.executeAsyncTask(i);
//        }
//        long endTime=System.currentTimeMillis(); //获取结束时间
//        logger.info("最终程序运行时间： "+(endTime-startTime)+"ms");
    }

    @Test
    public void show()throws Exception{
    }

    @Test
    public void randomCode() {

        StringBuilder str = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            str.append(random.nextInt(10));
        }
        System.out.println(str.toString());
    }

    /**
     * 测试日期所在的星期
     */
    @Test
    public void getWeek() {
        Date date = new Date();
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        System.out.println(weekDays[w]);
    }

    /**
     * 测试日期所在月的天数
     */
    @Test
    public void getMonthDays() throws ParseException {
        int num;
        String time = "2019-02-15";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(time);
//        Date date = new Date();
        if (date == null){
            num = 0;
            System.out.println(num);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, 1);// 把日期设置为当月第一天
        calendar.roll(Calendar.DATE, -1);// 日期回滚一天，也就是最后一天
        num = calendar.get(Calendar.DATE);
        System.out.println(num);
    }

    @Test
    public void testBetweenDays() throws ParseException {
        /**
         * 通过时间秒毫秒数判断两个时间的间隔
         * @param date1
         * @param date2
         * @return
         */
        String time = "2020-04-1";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse(time);
        Date date2 = new Date();
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
        System.out.println(days);
    }

    @Test
    public void testGetAddress() {
        String ip = "127.0.0.1";
        String address = IpUtils.getCityInfo(ip);
        System.out.println(address.replace("|0",""));
        System.out.println(address);
    }

    @Test
    public void testList() {
        List<Map<String,Object>> list = new ArrayList<>();
        if (list.isEmpty()) {
            System.out.println("空");
        } else {
            System.out.println(list.get(0));
        }
    }

}
